
package com.gitee.jmash.storage.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.gitee.jmash.core.orm.jpa.OrderBaseDao;
import com.gitee.jmash.core.orm.jpa.TenantEntityManager;
import com.gitee.jmash.storage.entity.StorageOrganUserEntity;
import com.gitee.jmash.storage.entity.StorageOrganUserEntity.StorageOrganUserPk;

/**
 * StorageOrganUser实体的Dao层（使用JPA实现）.
 *
 * @author <a href="mailto:service@crenjoy.com">crenjoy</a>
 */
public class StorageOrganUserDao extends OrderBaseDao<StorageOrganUserEntity, StorageOrganUserPk> {

  public StorageOrganUserDao() {
    super();
  }

  public StorageOrganUserDao(TenantEntityManager tem) {
    super(tem);
  }

  /**
   * 查询当前用户的组织成员信息.
   */
  public StorageOrganUserEntity findMyByOrganId(String unifiedId, UUID organId) {
    String sql =
        "select s from StorageOrganUserEntity s where s.unifiedId = ?1 and s.organId = ?2 order by s.orderBy asc ";
    return this.findSingle(sql, unifiedId, organId);
  }

  /**
   * 查询用户组织成员列表.
   */
  public List<StorageOrganUserEntity> findMyList(String unifiedId) {
    String sql =
        "select s from StorageOrganUserEntity s where s.unifiedId = ?1 order by s.orderBy asc ";
    return this.findList(sql, unifiedId);
  }

  /**
   * OrderBy增加1;
   */
  public int insOrderBy(String unifiedId) {
    String sql =
        "update StorageOrganUserEntity s set s.orderBy=s.orderBy+1 where s.unifiedId = ?1  ";
    return this.executeUpdate(sql, unifiedId);
  }

  /**
   * 获取最大排序值.
   */
  public int getMaxOrderBy(String unifiedId) {
    Map<String, String> params = new HashMap<>();
    params.put("unifiedId", unifiedId);
    return this.getMaxOrderBy(params);
  }

}
